package com.miracle.main.module.mvp.presenter;

import android.app.Activity;
import android.support.v4.app.Fragment;

import com.miracle.basemvp.base.BasePresenter;
import com.miracle.basemvp.base.BaseResponse;
import com.miracle.basemvp.base.IPresenter;
import com.miracle.basemvp.base.state.StateController;
import com.miracle.basemvp.lifecycle.rx.RxDefaultObserver;
import com.miracle.common.mvp.entity.response.BaseListResponse;
import com.miracle.common.util.CommonConstants;
import com.miracle.common.util.PreferencesUtils;
import com.miracle.common.view.RefreshView;
import com.miracle.main.module.mvp.contract.SecretContract;
import com.miracle.main.module.mvp.entity.bean.FileItem;
import com.miracle.main.module.mvp.entity.bean.SecretBean;
import com.miracle.main.module.mvp.entity.request.SecretListRequest;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * com.miracle.main.module.mvp.presenter
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/7 9:19
 * @see com.miracle.main.module.mvp.presenter
 */
public class SecretPresenter extends BasePresenter<SecretContract.View, SecretContract.Model>
        implements IPresenter {
    @Inject
    public SecretPresenter(SecretContract.View view, SecretContract.Model model) {
        super(view, model);
    }

    public void getList(LifecycleProvider<FragmentEvent> lifecycleProvider, SecretListRequest
            request, final StateController stateController) {
        String session = PreferencesUtils.getPreferences(stateController.mContext,
                PreferencesUtils.State
                        .USER_SESSIONID);
        request.setJSESSIONID(session);
        request.setUri(CommonConstants.URI.URI_SAY_LIST);
        request.setPageNo(mView.getCurrPage());
        request.setPageSize(mView.getPageSize());
        mModel.getList(request)
                .compose(lifecycleProvider
                        .<BaseResponse<BaseListResponse<SecretBean>>>bindUntilEvent
                                (FragmentEvent.DESTROY))
                .subscribe(new RxDefaultObserver<BaseResponse<BaseListResponse<SecretBean>>>
                        (stateController) {
                    @Override
                    public void onSucceed(BaseResponse<BaseListResponse<SecretBean>>
                                                  baseListResponseBaseResponse) {
                        stateController.onSuccessState(mView, baseListResponseBaseResponse,
                                (baseListResponseBaseResponse.getContent() != null) ?
                                        baseListResponseBaseResponse.getContent().getList() :
 null);
                    }
                });
    }

    public void getList(LifecycleProvider<FragmentEvent> lifecycleProvider, SecretListRequest
            request, RefreshView refreshView) {
        String session = PreferencesUtils.getPreferences(((Fragment) mView).getActivity(),
                PreferencesUtils.State
                        .USER_SESSIONID);
        request.setJSESSIONID(session);
        request.setUri(CommonConstants.URI.URI_SAY_LIST);
        request.setPageNo(refreshView.getCurrPage());
        request.setPageSize(refreshView.getPageSize());
        mModel.getList(request)
                .compose(lifecycleProvider
                        .<BaseResponse<BaseListResponse<SecretBean>>>bindUntilEvent
                                (FragmentEvent.DESTROY))
                .subscribe(refreshView.getListObserVer());
    }
}
